﻿Public Class frmTongHopOption
    Dim ctrlDonVi As New CDonViController
    Private Sub frmTongHopOption_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        BindDonViToCheckBox()
        txtNam.Focus()
        txtNam.Text = Date.Now.Year.ToString()
        txtNam.SelectAll()
    End Sub
    Private Sub BindDonViToCheckBox()
        chkDonVi.Items.Clear()
        Dim list = ctrlDonVi.SelectAllRecord()
        For Each dv As tblDonVi In list
            chkDonVi.Items.Add(dv.Ten)
        Next
    End Sub
    Private Function GetAllDonViChecked() As List(Of tblDonVi)
        Dim kq As New List(Of tblDonVi)
        Dim checked_items As CheckedListBox.CheckedItemCollection = chkDonVi.CheckedItems
        For Each item As Object In checked_items
            Dim ten As String = item.ToString()
            Dim dv As tblDonVi = ctrlDonVi.SelectRecordByName(ten)
            If (dv IsNot Nothing) Then
                kq.Add(dv)
            End If
        Next item
        Return kq
    End Function
    Private Function GetIDList(ByVal list As List(Of tblDonVi)) As String
        Dim kq As String = ""
        For Each dv As tblDonVi In list
            kq = kq & dv.ID_DonVi & ", "
        Next
        If kq <> "" Then
            kq = kq.Substring(0, kq.Length - 2)
        End If
        Return kq
    End Function
    Private Function GetTenList(ByVal list As List(Of tblDonVi)) As String
        Dim kq As String = ""
        For Each dv As tblDonVi In list
            kq = kq & dv.Ten & ", "
        Next
        If kq <> "" Then
            kq = kq.Substring(0, kq.Length - 2)
        End If
        Return kq
    End Function

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub

    Private Sub btPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btPrint.Click
        Dim list As List(Of tblDonVi) = GetAllDonViChecked()
        Dim sID As String = GetIDList(list)
        Dim sDV As String = GetTenList(list)
        Dim dt As New DataTable()
        XL_DULIEU.GanCauLenhKetNoi(Global.QLyNganSach.My.MySettings.Default.QLyNganSachConnectionString)
        XL_DULIEU.LayDuLieuRaBang("select * from view_TongHop ct where Nam=" & txtNam.Text & " and ID_DonVi in (" & sID & ")", dt)
        dt.TableName = "view_TongHop"
        Dim ds As New DataSet
        ds.Tables.Add(dt)
        'Dim tongTien As Double = 0
        'For Each r As DataRow In dt.Rows
        '    tongTien += Double.Parse(r("SoTien").ToString())
        'Next
        'Dim tienChu As String = CUtilities.NumToStringVietNam(tongTien)
        Dim rpt As New rptTongHop3
        rpt.SetDataSource(ds)

        rpt.SetParameterValue("TruongBan", txtTruongBan.Text)
        rpt.SetParameterValue("NguoiTongHop", txtNguoiTongHop.Text)
        rpt.SetParameterValue("DonVi", "Đơn vị: " & sDV)
        Dim frm As New frmView(rpt)
        frm.ShowDialog()
    End Sub
End Class